package com.hngy.scala.batch

import org.apache.flink.api.scala.ExecutionEnvironment

/**
  * 需求：统计指定文件中单词出现的总次数
  */
object BatchWordCountScala {
  def main(args: Array[String]): Unit = {
    //获取执行环境
    val env = ExecutionEnvironment.getExecutionEnvironment

    val inputPath = "hdfs://hadoop001:9001/word.txt"
    val outPath = "hdfs://hadoop001:9001/out"
    //读取文件中的数据
    val text = env.readTextFile(inputPath)

    //处理数据
    import org.apache.flink.api.scala._
    var wordCount = text.flatMap(_.split(" "))
        .map((_,1))
        .groupBy(0)
        .sum(1)
        .setParallelism(1)

    //将结果数据保存到文件中
    wordCount.writeAsCsv(outPath,"\n"," ")
    //wordCount.print()

    //执行程序
    env.execute("BatchWordCountScala");
  }
}
